package com.ctshk.app.system.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.req.UploadReq;
import com.ctshk.rpc.system.service.IFileOSSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.UUID;

/**
 * <p>
 * 文件服务 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-26
 */
@Api(value = "文件服务管理", tags = "文件服务管理")
@RestController
@RequestMapping("/file/oss")
public class FileOSSController extends BaseController {

    @DubboReference
    private IFileOSSService fileOSSService;

    @ApiOperation(value = "创建文件空间test", notes = "创建文件空间test")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "创建文件空间test", operDesc = "创建文件空间test")
    @GetMapping("/create/test")
    public Result create() {
        fileOSSService.createBucket();
        return Result.success();
    }

    @ApiOperation(value = "上传文件", notes = "上传文件")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "上传文件", operDesc = "上传文件")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile multipartFile, @RequestParam("businessType") int businessType) throws IOException {
        String fileName = multipartFile.getOriginalFilename();
        return fileOSSService.upload(fileName, multipartFile.getBytes(), businessType);
    }

    @ApiOperation(value = "上传文件列表test", notes = "上传文件列表test")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "上传文件列表test", operDesc = "上传文件列表test")
    @PostMapping("/upload/list")
    public Result uploadList(@RequestParam("files") MultipartFile[] multipartFiles) {
        if (multipartFiles != null) {
            ArrayList<UploadReq> uploadReqs = new ArrayList<>();
            for (MultipartFile multipartFile : multipartFiles) {
                UploadReq uploadReq = new UploadReq();
                try {
                    uploadReq.setBytes(multipartFile.getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                uploadReq.setFullName(multipartFile.getOriginalFilename());
                uploadReq.setOriginalFilename(multipartFile.getOriginalFilename());
                uploadReq.setContentType(multipartFile.getContentType());
                uploadReq.setSize(multipartFile.getSize());
                int symbolIndex = multipartFile.getOriginalFilename().indexOf(".");
                String suffix = multipartFile.getOriginalFilename().substring(symbolIndex);
                uploadReq.setFileSuffix(suffix);
                uploadReqs.add(uploadReq);
            }
            fileOSSService.uploadBatch(uploadReqs);
        }
        return Result.success();
    }

    @ApiOperation(value = "下载文件test", notes = "下载文件test")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "下载文件test", operDesc = "下载文件test")
    @GetMapping("/download/{objectName}/test")
    public Result download(@PathVariable("objectName") String objectName) {
        fileOSSService.download(objectName);
        return Result.success();
    }

    @ApiOperation(value = "文件列表test", notes = "文件列表test")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "文件列表test", operDesc = "文件列表test")
    @GetMapping("/list/test")
    public Result list() {
        fileOSSService.list();
        return Result.success();
    }

}
